package com.jianbing.entity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import io.netty.channel.Channel;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 模拟器
 * Created by Han, Tixiang on 2016/8/25 0025.
 */
public class Node {
    private Object id;
    private String type;
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    private Date createDate;
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    private Date updateDate;

    public void setDeleteDate(Date deleteDate) {
        this.deleteDate = deleteDate;
    }

    public Date getDeleteDate() {

        return deleteDate;
    }

    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    private Date deleteDate;
    private AtomicInteger totalNum = new AtomicInteger(-1);
    private AtomicInteger freeNum = new AtomicInteger();
    public void setRun(boolean run) {
        this.run = run;
    }

    public boolean isRun() {

        return run;
    }
    private volatile boolean run = true;


    public void setUpdateDate(Date updateDate) {
        this.updateDate = updateDate;
    }

    public Date getUpdateDate() {

        return updateDate;
    }

    public void setCreateDate(Date createDate) {

        this.createDate = createDate;
    }

    public Date getCreateDate() {
        return createDate;
    }


    @JSONField(serialize = false)
    private Channel channel;

    public void setChannel(Channel channel) {
        this.channel = channel;
    }

    public Channel getChannel() {
        return channel;
    }

    public Object getId() {
        return id;
    }

    public String getType() {
        return type;
    }

    public void setId(Object id) {
        this.id = id;
    }

    public void setType(String type) {
        this.type = type;
    }

    public void setTotalNum(AtomicInteger totalNum) {
        this.totalNum = totalNum;
    }

    public void setFreeNum(AtomicInteger freeNum) {
        this.freeNum = freeNum;
    }

    public AtomicInteger getTotalNum() {
        return totalNum;
    }

    public AtomicInteger getFreeNum() {
        return freeNum;
    }

    public boolean reduceFreeNum() {
        return reduceFreeNum(1);
    }

    public boolean release() {
        return addFreeNum(1);
    }

    public boolean reduceFreeNum(int num) {
        synchronized (this) {
            if (freeNum.get() - num >= 0) {
                freeNum.addAndGet(-num);
                return true;
            }
        }
        return false;
    }


    public boolean addFreeNum(int num) {
        synchronized (this) {
            if (freeNum.get() + num <= totalNum.get()) {
                freeNum.addAndGet(num);
                return true;
            }
        }
        return false;
    }

    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

    public String toShortString() {
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        sb.append("id=");
        sb.append(this.getId());
        sb.append(",type=");
        sb.append(this.getType());
        sb.append("]");
        return sb.toString();
    }

    public static void main(String[]args){
        Node node = JSON.parseObject("{\"_id\":-1,\"freeNum\":5,\"id\":\"1\",\"totalNum\":5,\"type\":\"yzm\"}",Node.class);
        System.out.println(node.getId());
    }
}
